package com.traffic.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.traffic.constant.Status;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.traffic.entity.Label;
import com.traffic.mapper.LabelMapper;
import com.traffic.service.LabelService;

@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService{

    @Autowired
    LabelMapper labelMapper;

    @Override
    public List<Label> queryTargetLabel(String startDate,String endDate,String type,String pass,String address) throws ParseException {

        startDate = startDate.replace("Z"," UTC");
        endDate = endDate.replace("Z"," UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date stime = new Date();
        Date etime = new Date();

        if(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            stime = format.parse(startDate);
            etime = format.parse(endDate);
        }
        else{
            stime = format.parse("2020-01-14T00:00:00.000 UTC");
            etime = format.parse("2034-01-14T00:00:00.000 UTC");
        }
        final String sresult = defaultFormat.format(stime);
        final String eresult = defaultFormat.format(etime);

        QueryWrapper<Label> wrapper = new QueryWrapper<>();
        //  wrapper.like("question",ques).between("create_time",startDate,endDate);
        wrapper.like("type",type).
                like("pass",pass).
                like("address",address).
                and(StringUtils.isNotBlank(startDate)
                        && StringUtils.isNotBlank(endDate),
//                dateSql -> dateSql.ge("createtime", startDate.toString())
//                        .apply("createtime <= DATE_ADD({0}, INTERVAL 1 DAY)", endDate.toString()));
                        dateSql ->dateSql.between("createtime",sresult,eresult));
        List<Label> labels = labelMapper.selectList(wrapper);

        return labels;
    }
}
